package com.example.gustavo.hello.activity;

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.graphics.Color;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.TextView;

import com.avos.avoscloud.AVOSCloud;
import com.example.gustavo.hello.R;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;

public class RecyclerViewActivity extends AppCompatActivity {

    private static final String TAG = "RecyclerViewActivity";
    private RecyclerView recyclerView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_view);

        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);

        dragRecyclerView();

    }

    /**
     * 利用ItemTouchHelper实现可拖动
     */
    private void dragRecyclerView(){
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
//        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3);
        recyclerView.setLayoutManager(linearLayoutManager);

        List<String> dataList = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            dataList.add("text" + i);
        }
        MyAdapter myAdapter = new MyAdapter(dataList);
        recyclerView.setAdapter(myAdapter);

        //利用ItemTouchHelper类来实现RecyclerView的drag/swipe to dismiss
        //list
        int dragFlag = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
        int swipeFlag = ItemTouchHelper.START | ItemTouchHelper.END;
        //grid
//        int dragFlag = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
//        int swipeFlag = 0;
        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(new MCallBack(dragFlag, swipeFlag, myAdapter));
        itemTouchHelper.attachToRecyclerView(recyclerView);
        myAdapter.setItemTouchHelper(itemTouchHelper);
    }

    interface ItemDragSwipe {

        void onMove(int from, int to);

        void onSwiped(int position);
    }

    class MCallBack extends ItemTouchHelper.SimpleCallback{
        private ItemDragSwipe itemDragSwipe;


        public MCallBack(int dragDirs, int swipeDirs, ItemDragSwipe itemDragSwipe) {
            super(dragDirs, swipeDirs);
            this.itemDragSwipe = itemDragSwipe;
        }

        @Override
        public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
            itemDragSwipe.onMove(viewHolder.getAdapterPosition(), target.getAdapterPosition());
            return true;
        }

        @Override
        public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {

            itemDragSwipe.onSwiped(viewHolder.getAdapterPosition());
        }

        @Override
        public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {

            if(actionState != ItemTouchHelper.ACTION_STATE_IDLE){

                if(viewHolder instanceof MyViewHolder){

                    MyViewHolder holder = (MyViewHolder) viewHolder;
                    holder.itemView.setBackgroundColor(getResources().getColor(R.color.holo_blue_light));
                }
            }
            super.onSelectedChanged(viewHolder, actionState);
        }

        @Override
        public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
            super.clearView(recyclerView, viewHolder);
            if(viewHolder instanceof MyViewHolder){

                MyViewHolder holder = (MyViewHolder) viewHolder;
                holder.itemView.setBackgroundColor(Color.TRANSPARENT);
            }
        }



    }


    class MyAdapter extends RecyclerView.Adapter<MyViewHolder> implements ItemDragSwipe{

        private List<String> list;
        private ItemTouchHelper itemTouchHelper;

        public MyAdapter(List<String> dataList) {
            list = dataList;
        }

        public void setItemTouchHelper(ItemTouchHelper itemTouchHelper) {
            this.itemTouchHelper = itemTouchHelper;
        }

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {


            return new MyViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recyler, parent, false));
        }

        @Override
        public void onBindViewHolder(final MyViewHolder holder, int position) {

            holder.itemView.setBackgroundColor(0);
            holder.tv.setText(list.get(position));

            if (itemTouchHelper != null) {

                holder.iv.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {

                        itemTouchHelper.startDrag(holder);
                        return false;
                    }
                });
            }
        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        @Override
        public void onMove(int from, int to) {

            Collections.swap(list, from, to);
            notifyItemMoved(from, to);

        }

        @Override
        public void onSwiped(int position) {

            list.remove(position);
            notifyItemRemoved(position);
        }

        public void onSelected(){

        }

    }


    static class MyViewHolder extends RecyclerView.ViewHolder{

        TextView tv;
        ImageView iv;

        public MyViewHolder(View itemView) {
            super(itemView);
            tv = (TextView) itemView.findViewById(R.id.tv);
            iv = (ImageView) itemView.findViewById(R.id.iv);
        }


    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_recycler_view, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
